<template>
  <Dialog :title="title" :model-value="visible" width="500px" :before-close="closeForm">
    <Form
      :schema="schema"
      :model="dic"
      @register="formRegister"
      label-width="120px"
      :isCol="false"
      :rules="rules"
    />
    <template #footer>
      <el-space>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-space>
    </template>
  </Dialog>
</template>

<script>
import { HrItemDic } from '@/config/publicDic'
import * as dicApi from '@/api/base/dict'
import { Dialog } from '@/components/Dialog'
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
const { formRegister, formMethods } = useForm()
export default {
  name: 'EditDic',
  components: {
    Form,
    Dialog
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      title: '新增字典',
      dic: {},
      source: null,
      schema: [
        {
          field: 'DicName',
          label: '字典名',
          component: 'Input',
          componentProps: {
            maxlength: 100,
            placeholder: '字典名'
          }
        },
        {
          field: 'DicClassify',
          label: '字典分类',
          component: 'TreeDicItem',
          componentProps: {
            dicId: HrItemDic.DicClassify,
            placeholder: '选择字典分类'
          }
        },
        {
          field: 'Remark',
          label: '说明',
          component: 'Input',
          componentProps: {
            maxlength: 255,
            type: 'textarea',
            placeholder: '说明'
          }
        }
      ],
      rules: {
        DicClassify: [
          {
            required: true,
            message: '字典分类不能为空！',
            trigger: 'blur'
          }
        ],
        DicName: [
          {
            required: true,
            message: '字典名不能为空！',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 50,
            message: '字典名长度在 2 到 50 个字之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  methods: {
    formRegister,
    async save() {
      const data = await formMethods.validate()
      if (data == null) {
        return
      } else if (this.id) {
        this.setDic(data)
      } else {
        this.addDic(data)
      }
    },
    async setDic(data) {
      await dicApi.Set(this.id, data)
      this.$message({
        message: '更新成功!',
        type: 'success'
      })
      this.$emit('update:visible', false)
      this.$emit('close', true)
    },
    async addDic(data) {
      await dicApi.Add(data)
      this.$message({
        message: '添加成功!',
        type: 'success'
      })
      this.$emit('update:visible', false)
      this.$emit('close', true)
    },
    async reset() {
      if (this.id == null) {
        this.title = '新增字典'
        this.source = null
        this.dic = {}
      } else {
        const data = await dicApi.Get(this.id)
        this.source = data
        this.dic = data
        this.title = '编辑字典-' + data.DicName
      }
      formMethods.setValues(this.dic)
    },
    closeForm() {
      this.$emit('update:visible', false)
      this.$emit('close', false)
    },
    resetForm() {
      if (this.id == null) {
        this.dic = {}
      } else {
        this.dic = this.source
      }
      formMethods.setValues(this.dic)
    }
  }
}
</script>
